#include <iostream>
#include <string>

#include <algorithm>
#include <vector>
using namespace std;


    int maxnumber(int n)
    {
        int Max = 0;
        while (n)
        {
            int a = n % 10;
            Max = max(Max, a);
            n /= 10;
        }
        return Max;
    }

    int maxSum(int* nums,int length) {
        sort(nums, nums+length);
        int Max = 0;
        for (int i = length - 1; i > 0; i--)
        {
            for (int j = i - 1; j >= 0; j--)
            {
                if (maxnumber(nums[i]) == maxnumber(nums[j]))
                {
                    Max = max(Max, nums[i] + nums[j]);
                }
            }
        }
        return Max > 0 ? Max : -1;
    }


int main()
{
    int nums[5] = { 51,71,17,24,42 };
    int length = sizeof(nums) / sizeof(int);
    int sum = maxSum(nums,length);
    printf("%d", sum);
    return 0;
}